package edu.mju.eduservice.controller;


import edu.mju.commonutils.ReturnCode;
import edu.mju.eduservice.entity.StudyVideo;
import edu.mju.eduservice.service.StudyVideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author xiaoxin
 * @since 2020-08-31
 */
@RestController
@RequestMapping("/eduservice/study-video")
public class StudyVideoController {

    @Autowired
    private StudyVideoService studyVideoService;


    /**
     * 添加小节
     * @param studyVideo
     * @return
     */
    @PostMapping("/addVideo")
    public ReturnCode addVideo(@RequestBody StudyVideo studyVideo){
        studyVideoService.save(studyVideo);
        return ReturnCode.success();
    }


    /**
     * 删除小节
     * @param id
     * @return
     */
    @DeleteMapping("/deleteVideo/{id}")
    public ReturnCode deleteVideo(@PathVariable String id){
        //根据小节id获取视频id
        StudyVideo studyVideo = studyVideoService.getById(id);
        String videoId = studyVideo.getVideoSourceId();

        //判断是否有视频id
        if(!StringUtils.isEmpty(videoId)){
            //根据id删除视频
            studyVideoService.deleteVideoById(videoId);
        }

        //删除小节
        studyVideoService.removeById(id);
        return ReturnCode.success();
    }

    /**
     * 修改小节
     * @param studyVideo
     * @return
     */
    @PostMapping("/updateVideo")
    public ReturnCode updateVideo(@RequestBody StudyVideo studyVideo){
        studyVideoService.updateById(studyVideo);
        return ReturnCode.success();
    }


    /**
     * 根据小节id查询小节基本信息
     * @param id 小节id
     * @return
     */
    @GetMapping("/getVideo/{id}")
    public ReturnCode getVideoInfo(@PathVariable String id){
        StudyVideo studyVideo = studyVideoService.getById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("studyVideo",studyVideo);
        return ReturnCode.success().data(map);
    }



}

